<template>
  <div>
    <template>
      <el-button icon="el-icon-edit" @click="dialogVisible = true">新增</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="codeingId" label="代码id" width="180"></el-table-column>
        <el-table-column prop="codeingContent" label="代码内容" width="180"></el-table-column>
        <el-table-column prop="teachTitle" label="教材标题" width="180"></el-table-column>
        <el-table-column prop="createTime" label="发布时间" width="180"></el-table-column>
        <!-- <el-table-column prop="codeingAdjunct" label="附件" width="180"></el-table-column> -->
        <!-- codeingAdjunct -->
        <el-table-column label="附件">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="goCodeing(scope.row)">查看教学代码</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--  <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button>
            <el-button size="mini" type="danger" @click="deleteAll(scope.$index, scope.row)">删除教材</el-button> -->
            <!--            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template>
      <el-pagination small layout="total, prev, pager, next" :page-count="pages" :page-size="size"
        :current-page="current" :total="total" @current-change="pageheigerGb">
      </el-pagination>
    </template>



    <div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" >
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="教材标题" prop="tid">
            <el-input type="text" v-model="ruleForm.tid" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="代码块内容" prop="content">
            <el-input type="text" v-model="ruleForm.content" autocomplete="off"></el-input>
          </el-form-item>
          <!-- <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload> -->
          <el-upload action="http://localhost:8001/tbCodeing/uploadSescc" list-type="picture-card" :auto-upload="true"
            :on-success="onss">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                  <i class="el-icon-download"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>




        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [],
        total: 0,
        size: 0,
        pages: 0,
        current: 0,
        dialogVisible: false,
        ruleForm: {
          tid: "",
          content: "",
          url : ""
        },
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false
      }
    },
    methods: {
      onss(file) {
        console.log(file.data)
        this.ruleForm.url = file.data
      },
      handleRemove(file) {
        console.log(file);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleDownload(file) {
        console.log(file);
      },
      add() {
        let param = {
          "codeingContent": this.ruleForm.content,
          "teachId": this.ruleForm.tid,
          "codeingAdjunct" :this.ruleForm.url
        }
        console.log(param)
        this.$http.post("http://localhost:8001/tbCodeing/insertCodeIng", param).then(res => {
          if (res.data.code === 200) {
            alert("新增成功")
            this.dialogVisible = false
          }
        })
      },
      goCodeing(row) {
        console.log(row);
        let data = new FormData();
        data.append("filename", row.codeingAdjunct)
        this.$http.post("http://localhost:8001/tbCodeing/downlodSescc", data).then(res => {
          if (res.data.code === 200) {
            alert("下载成功")
          }
        })
      },
      pageheigerGb(val) {
        let teachId = sessionStorage.getItem("teachId");
        let param = {
          "current": val,
          "size": 3,
          "paramMap": {
            "teach": outlineId
          }
        }
        this.$http.post("http://localhost:8001/tbCodeing/selectCodeIng", param).then(res => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records;
            this.total = parseInt(res.data.data.total)
            this.size = parseInt(res.data.data.size)
            this.pages = parseInt(res.data.data.pages)
            this.current = parseInt(res.data.data.current)
          }
        })
      }
    },
    mounted() {
      let teachId = sessionStorage.getItem("teachId");
      let param = {
        "current": 1,
        "size": 3,
        "paramMap": {
          "teach": teachId
        }
      }
      if (teachId === null || teachId == 0) {
        alert("请选择对应教材")
        this.$router.push("teach")
      } else {
        this.$http.post("http://localhost:8001/tbCodeing/selectCodeIng", param).then(res => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records;
            this.total = parseInt(res.data.data.total)
            this.size = parseInt(res.data.data.size)
            this.pages = parseInt(res.data.data.pages)
            this.current = parseInt(res.data.data.current)
          }
        })
      }

    }
  }
</script>
